package utils

import (
	"sync"
)

// LoadManager 负载管理对象
type LoadManager struct {
	maxRequests     int
	currentRequests int
	mu              sync.Mutex
}

// NewLoadManager 创建一个负载管理实例
func NewLoadManager(maxRequests int) *LoadManager {
	return &LoadManager{
		maxRequests: maxRequests,
	}
}

// IncreaseRequest 增加请求数
func (lm *LoadManager) IncreaseRequest() bool {
	lm.mu.Lock()
	defer lm.mu.Unlock()
	if lm.currentRequests < lm.maxRequests {
		lm.currentRequests++
		return true
	}
	return false
}

// DecreaseRequest 减少请求数
func (lm *LoadManager) DecreaseRequest() {
	lm.mu.Lock()
	defer lm.mu.Unlock()
	if lm.currentRequests > 0 {
		lm.currentRequests--
	}
}
